#Installing Packages

library(tidyverse)
library(ggalluvial)
library(ggthemes)
library(viridis)
library(patchwork)
library(dataRetrieval)
library(dplyr) # for `rename` & `select`
library(tidyr) # for `gather`
library(ggplot2)
library(extrafont)

#Setup for beautiful grpahs
windowsFonts(A = windowsFont("Times New Roman"))


##################################################################################
# 1 Sankey for participation
##################################################################################

# load data
data <- read.csv("C:/Users/vierus/OneDrive - Universität Duisburg-Essen/Pulse Survey Studis WS 2122/Analyse/_ready_for_Dataverse/sankey/sankey_data_participation.csv")

# calculate all combinations, make unique id and save
data2 <- count(data, t1, t2, t3, t4) %>% 
  mutate(id = row_number()) # create new id variable

# make long data
data3 <- gather(data2, value, key, -n, -id) 

# look at data
head(data3)


data_anwesenheit <- data3 %>%
  mutate(wave = as.numeric(str_remove(value, "t")), 
         key1 = as.character(key),
         key2 = as.factor(key1),
         key3 = fct_relevel(key2, "Nicht Anwesend", "Anwesend", after = 0)) 

fig1 <- data_anwesenheit %>%
  ggplot(aes(
    x = wave,
    stratum = key2,
    alluvium = id,
    y = n,
    fill = key2
  )) +
  geom_flow() +
  geom_stratum(alpha = .5) +
  theme_tufte(base_size = 18) +
  theme(text = element_text(family = "A")) +
  theme(legend.title=element_text(size=16)) +
  theme(legend.text=element_text(size=16)) +
  theme(axis.title.x = element_text(size =16)) +
  theme(axis.title.y = element_text(size = 16)) +
  theme(axis.text.x = element_text(size = 16)) +
  theme(axis.text.y = element_text(size = 16)) +
  labs(x = "Wave",
       y = "Frequency",
       fill = "Outcome") +
  scale_fill_manual(values=c("#D3D3D3", "#000000"), 
                    name="Participation",
                    labels=c("No", "Yes"))  
  
fig1

#export as eps
ggsave(file = "Sankey_participation.eps")                     

##################################################################################
# 2 Sankey for satisfaction
##################################################################################

# load local data
data <- read.csv("C:/Users/vierus/OneDrive - Universität Duisburg-Essen/Pulse Survey Studis WS 2122/Analyse/_ready_for_Dataverse/sankey/sankey_data_satisfaction.csv")

# have a look at our data
head(data)

# calculate all combinations, make unique id and save
data2 <- count(data, t1, t2, t3, t4) %>% 
  mutate(id = row_number()) # create new id variable

# look at data
head(data2)

# make long data
data3 <- gather(data2, value, key, -n, -id) 

# look at data
head(data3)

data_zufriedenheit <- data3 %>%
  mutate(wave = as.numeric(str_remove(value, "t")), 
         key1 = as.character(key),
         key2 = as.factor(key1),
         key3 = fct_relevel(key2, "Nicht Anwesend", "Anwesend", after = 0)) 


fig2 <- data_zufriedenheit %>%
  ggplot(aes(
    x = wave,
    stratum = key2,
    alluvium = id,
    y = n,
    fill = key2
  )) +
  geom_flow() +
  geom_stratum(alpha = .5) +
  theme_tufte(base_size = 14) +
  theme(text = element_text(family = "A")) +
  theme(legend.title=element_text(size=16)) +
  theme(legend.text=element_text(size=16)) +
  theme(axis.title.x = element_text(size =16)) +
  theme(axis.title.y = element_text(size = 16)) +
  theme(axis.text.x = element_text(size = 16)) +
  theme(axis.text.y = element_text(size = 16)) +
  labs(x = "Wave",
       y = "Frequency",
       fill = "Outcome") +
      scale_fill_manual(values=c("#000000", "#848484", "#FFFFFF"), 
                  name="Satisfaction",
                  labels= c("High (5.1 - 7)", "Medium (3.1 - 5)", "Low (1 - 3)")) 

fig2
ggsave(file = "Sankey_satisfaction.eps")                     

  
